define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    $("#c-testpaper_id").data('params',function(){
        return {
            custom:{
                course_id:$("#c-course_id").val()
            }
            // "custom[course_id]":"3"
        }
    })

    function dissMinutes(){
        var start = $('#c-starttime').val()
        if(!start) return
        var end = $('#c-endtime').val()
        if(!end) return

        start = new Date(start).getTime()
        end = new Date(end).getTime()

        var m = end - start

        var minutes = Math.floor(m/1000/60)
        
        if(minutes<=10){
            layer.msg("考试时间不能低于10分钟")
            return false
        }
        else if(minutes>150){
            layer.msg("考试时间不能超过150分钟")
            return false
        }
        
        $('#c-minutes').val(minutes)
    }

    $('#c-starttime').on('dp.change',function(){
        console.log('时间改变了');
        dissMinutes()
    })

    $('#c-endtime').on('dp.change',function(){
        console.log('时间改变了');
        dissMinutes()
    })

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exam/index' + location.search,
                    add_url: 'exam/add',
                    edit_url: 'exam/edit',
                    del_url: 'exam/del',
                    multi_url: 'exam/multi',
                    import_url: 'exam/import',
                    table: 'exam',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedNumber: 1,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'course_id', title: __('Course_id'),visible:false},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'testpaper_id', title: __('Testpaper_id'),visible:false},
                        {field: 'course.name', title: __('Course.name'), operate: 'LIKE'},
                        {field: 'testpaper.title', title: __('Testpaper.title'), operate: 'LIKE'},

                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'minutes', title: '考试时长', operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')},custom:{1:'primary',2:'danger',3:'success'}, formatter: function(value, row, index){
                            // console.log('row: ', row);
                            var h = Table.api.formatter.status.call(this,value, row, index)
                            if(Config.can_change_status){
                                h+=' <button class="btn btn-xs btn-success btn-status-pass" data-id="'+row.id+'">通过</button> <button class="btn btn-xs btn-danger btn-status-reject" data-id="'+row.id+'">拒绝</button>'
                            }
                            return h
                        }},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: function(v){
                            return v?v:'-'
                        }},
                        {field: 'admin_id', title: __('Admin_id'),visible:false},
                       
                        {field: 'admin.username', title: __('Admin.username'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });


            // 审核
            var audit = {
                pass:function(id){
                    $.ajax({
                        type: "post",
                        url: "./exam/change_status/type/pass",
                        data: {
                            id:id
                        },
                        dataType: "json",
                        success: function (response) {
                            console.log('response: ', response);
                            if(response.code == 1){
    
                                table.bootstrapTable('refresh')
                            }
                            else{
                                layer.msg(response.msg)
                            }
                        }
                    });
                },
                reject:function(id){
                    layer.prompt({title: '请输入拒绝原因', formType: 2}, function(val, index){
                        // 
                        if(val.trim()!==''){
      
                          $.ajax({
                              type: "post",
                              url: "./exam/change_status/type/reject",
                              data: {
                                  id:id,
                                  remark:val.trim()
                              },
                              dataType: "json",
                              success: function (response) {
                                  console.log('response: ', response);
                                  
                                  if(response.code == 1){
                                      layer.close(index);
                                      table.bootstrapTable('refresh')
                                  }
                                  else{
                                      layer.msg(response.msg)
                                  }
                              }
                          });
                        }
      
                      });
                }
            }
            // 批量通过
            $('.btn-audit-pass').click(function (e) { 
                e.preventDefault();
                var ids = Table.api.selectedids(table)
                audit.pass(ids)
            })

            // 批量拒绝
            $('.btn-audit-reject').click(function (e) { 
                e.preventDefault();
                var ids = Table.api.selectedids(table)
                audit.reject(ids)
            })

            // 通过
            $(document).on('click','.btn-status-pass',function(){
                var id = $(this).data('id');
                console.log('通过id: ', id);
                audit.pass(id)
            })

            // 拒绝
            $(document).on('click','.btn-status-reject',function(){
                var id = $(this).data('id');
                console.log('拒绝id: ', id);
                audit.reject(id)
            })


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'exam/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'exam/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'exam/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
